package com.j.lemon.learn.leecode;

import org.apache.commons.lang.StringUtils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * @Author lijunjun
 * @Date 2019-11-05 15:49
 * @Description
 * 给定一个字符串 s，找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。
 *
 * 示例 1：
 *
 * 输入: "babad"
 * 输出: "bab"
 * 注意: "aba" 也是一个有效答案。
 * 示例 2：
 *
 * 输入: "cbbd"
 * 输出: "bb"
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/longest-palindromic-substring
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 */
public class 最长回文子串 {
    public static void main(String[] args) {
//        String s = "9";
//        System.out.println(s.charAt(1));
//        System.out.println(longestPalindrome(s));
        getHostIp();
    }


    //扩展中心法
    public static String longestPalindrome1(String s) {
        if (s.length() < 2) {
            return s;
        }
        if(s.length() == 2){
            if(s.charAt(0) == s.charAt(1)){
                return s;
            }else{
                return String.valueOf(s.charAt(0));
            }
        }
        String result = "";

        for (int i = 0; i < s.length()-1; i++) {
            int l = 0;
            while (i>=0 && l<s.length()-1 && s.charAt(i)==s.charAt(l)){
                i--;
                l++;
            }
        }

        return result;
    }




    //暴力破解大法,会超出时间限制
    public static String longestPalindrome(String s) {
        if(s.length()<2){
            return s;
        }
        String result = "";
        for (int i = 0; i < s.length()-1; i++) {
            String temp = String.valueOf(s.charAt(i));
            for (int j = i; j < s.length()-1; j++) {
                String checkStr = temp+s.charAt(j+1);
                int l = 0;
                boolean flag = true;
                for (int k = checkStr.length()-1; k >l; l++) {
                    if(checkStr.charAt(l)!=checkStr.charAt(k)){
                        flag=false;
                        break;
                    }
                    k--;
                }
                if(flag && checkStr.length()>result.length()){
                    result = checkStr;
                }
                temp = checkStr;
            }
        }
        if("".equals(result)){
            return String.valueOf(s.charAt(0));
        }
        return result;

    }

    private static String getHostIp(){
        try{
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()){
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()){
                    InetAddress ip = (InetAddress) addresses.nextElement();
                    if (ip != null
                            && ip instanceof Inet4Address
                            && !ip.isLoopbackAddress() //loopback地址即本机地址，IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
                            && ip.getHostAddress().indexOf(":")==-1){
                        System.out.println("本机的IP = " + ip.getHostAddress());
                        return ip.getHostAddress();
                    }
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

}
